const jwt = require("jwt-simple");

module.exports = {
    Auth() {
        return function (req, res, next) {
            //1,去请求的4个地方找是否写带了token
            var token = req.query.token || req.body.token || req.cookies.token || req.headers.token;

            try {
                var oUser = jwt.decode(token, require("../configs").tokenAuth);
                req[require("../configs").currentUser] = oUser;
                next();

            } catch (error) {
                res.json({
                    msg: "token失效,请重新登录!",
                    code: -200
                })
            }

        }
    }
}